package com.wxf.web;

import com.wxf.service.StudentService;
import com.wxf.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/GetStudentList")
public class GetStudentListServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Obtain student info servlet");
        // 1-获取请求参数
        // 1.1-获取查询学生名称、学号、性别参数
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");
        String sex = req.getParameter("sex");
        sex=(sex==null?"-1":sex);
        // 1.2-获取客户端所选择的页码
        String pageIndex = req.getParameter("pageIndex");
        System.out.println(pageIndex);
        pageIndex=(pageIndex==null?"1":pageIndex);
        System.out.println(pageIndex);
        int pageNum = 5; // 每页显示条数
        //-----------------------------------------------------------------
        // 2-后台处理
        // 2.1-处理得到学生信息（全部信息和模糊查询）
        StudentService studentService = new StudentServiceImpl();
        List students = studentService.getStudentList(stuName,stuNo,Integer.parseInt(sex),Integer.parseInt(pageIndex),pageNum);
        // 2.2-获取总条数
        System.out.println(stuName+","+stuNo+","+sex);
        int totalDataNumber = studentService.getTotalDataNumber(stuName,stuNo,Integer.parseInt(sex));
        System.out.println(totalDataNumber);
        // 2.3-计算总页数
        int totalPageNum = (totalDataNumber%pageNum>0?1:0)+totalDataNumber/pageNum;

        // 3-响应
        // 后台给前台传值，要先存值
        req.setAttribute("studentList",students);
        req.setAttribute("stuName",stuName);
        req.setAttribute("stuNo",stuNo);
        req.setAttribute("sex",sex);
        req.setAttribute("currentPageIndex",pageIndex);
        req.setAttribute("totalDataNum",totalDataNumber);
        req.setAttribute("totalPageNum",totalPageNum);
        // 注意下面的路径不用加"/"，因为请求路径里有"Educational/student/",它会从这个路径下面去找资源
        // 如果加了"/"，表示从根目录（即web目录）去找资源
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
